package com.tjbklx1.zk.zookeeper.createSessionASync;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class MyWatcher implements Watcher {

	private ZooKeeper zooKeeper;

	public MyWatcher() {
		
	}
	
	public void setZooKeeper(ZooKeeper zooKeeper){
		this.zooKeeper = zooKeeper;
	}

	@Override
	public void process(WatchedEvent event) {
		System.out.println("watcher event = " + event);

		if (event.getState().equals(KeeperState.SyncConnected)) {
			doSomething(zooKeeper);
		}
	}

	private void doSomething(ZooKeeper zooKeeper) {
		System.out.println("doSomethings");
		zooKeeper.create("/node5", "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new IStringCallBack(),
				"create");

	}

}
